<template>
	<view class="content">
		<image src="@/static/img/apply_img.png" class="liu" mode="widthFix"></image>
		<view class="notice">
			<image src="../../static/img/apply_icon.png" mode="widthFix"></image>
			<uni-notice-bar scrollable background-color="#FFF3EE" :speed="speed" color="#FF534E" :text="text" />
		</view>
		<view class="money">
			<text>￥</text>
			<input type="number" :class="{'col_9':disabled}" @blur="checkInput" :disabled="disabled" v-model="money" placeholder="请输入贷款金额"
				placeholder-class="pla_money" />
		</view>
		<view class="hint">单笔最低{{money_min}}元，最高{{money_max}}元，需为50的倍数</view>
		<view class="info">
			<view class="item">
				<view>借款人姓名</view>
				<input type="text" placeholder="请输入借款人姓名" :class="{'col_9':disabled}" :disabled="disabled" v-model="name" />
			</view>
			<view class="item">
				<view>身份证号</view>
				<input type="text" placeholder="请输入身份证号" :class="{'col_9':disabled}" :disabled="disabled" v-model="card" />
			</view>
			<view class="item">
				<view>手机号</view>
				<input type="text" placeholder="请输入手机号" :class="{'col_9':disabled}" :disabled="disabled" v-model="phone" />
			</view>
			<view class="item">
				<view>借款用途</view>
				<!-- <view class="item_c">车押贷</view>
				<image src="@/static/img/la.png" mode="widthFix"></image> -->
				<uni-data-select v-model="value" :localdata="range" @change="change"></uni-data-select>
			</view>
			<view class="item">
				<view>借款周期</view>
				<uni-data-select v-model="value3" :localdata="range2" @change="changez"></uni-data-select>
			</view>
			<view v-if="value3==2" :class="value3!=3?'item' : 'jie'">
				<view>自定义周期(2-31天)</view>
				<view class="item_c lv">
					<input type="number" placeholder="请输入周期天数" v-model="day_demo" />天/周
				</view>
			</view>
			<view v-if="value3" :class="value3!=3?'item' : 'jie'">
				<view>借款期限</view>
				<view v-if="value3==1" class="item_c lv">
					<input type="number" placeholder="请输入借款期限" v-model="day" />天
				</view>
				<view v-if="value3==2" class="item_c lv">
					<input type="number" placeholder="请输入借款期限" v-model="week" />周
				</view>
			</view>
			<view class="yues" v-if="value3==3">
				<view :class="item==month?'sel':'no'" @click="month=item" v-for="(item,i) in monthList">{{item}}个月
				</view>
			</view>
			<view class="item">
				<view>每期利息</view>
				<view class="item_c lv"><input type="number" placeholder="请输利息" v-model="rate" />元</view>
			</view>
			<!-- <view class="hint">{{hint}}</view> -->
			<view class="hint" v-if="value3==1">日利率：{{(((rate*365/money)/365)*100||0).toFixed(2)||0}}
				%；年利率：{{((rate*365/money)*100||0).toFixed(2)||0}}%
			</view>
			<view class="hint" v-if="value3==2">日利率：{{((((rate/day_demo)*365/money)/365 )*100||0).toFixed(2)||0}}
				%；年利率：{{(((rate/day_demo)*365/money)*100||0).toFixed(2)||0}}%
			</view>
			<view class="hint" v-if="value3==3">日利率：{{((((rate/30)*365/money)/365)*100 ||0).toFixed(2)||0}}
				%；年利率：{{(((rate/30)*365/money)*100||0).toFixed(2)||0}}%
			</view>
			<view class="item">
				<view>还款方式</view>
				<uni-data-select v-model="value2" :localdata="repayment" @change="change2"> </uni-data-select>
			</view>
			<!-- <view class="item">
				<view>担保机构</view>
				<view class="item_c">不邀约</view>
				<image src="@/static/img/la.png" mode="widthFix"></image>
			</view> -->
			<!-- <view class="jie">筹标期限</view>
			<view class="yues">
				<view :class="i==0?'sel':'no'" v-for="(item,i) in 4">3天</view>
			</view> -->
			<view class="item" v-if="huanType==0">
				<view>{{value3==3?'每月还本息':value3==2? '每周还本息':'每日还本息'}}</view>
				<!-- <view class="item_c col_red">￥{{((money/month)+(money*(rate/100)/month))}}</view> -->
				<view v-if="value3==3" class="item_c col_red">￥{{((money/month)+(rate/100)*100||0).toFixed(2)||0}}</view>
				<view v-if="value3==2" class="item_c col_red">￥{{((money/week)+(rate/100)*100||0).toFixed(2)||0}}</view>
				<view v-if="value3==1" class="item_c col_red">￥{{((money/day)+(rate/100)*100||0).toFixed(2)||0}}</view>
			</view>
			<view class="item" v-if="huanType==1">
				<view>{{value3==3?'每月还利息':value3==2? '每周还利息':'每日还利息'}}</view>
				<!-- <view class="item_c col_red">￥{{(money*(rate/100)/month).toFixed(2)}}</view> -->
				<view class="item_c col_red">￥{{rate}}</view>
			</view>
			<view class="item" v-if="huanType==2">
				<view>到期还息</view>
				<!-- <view class="item_c col_red">￥{{(money*(rate/100)).toFixed(2)}}</view> -->
				<view v-if="value3==3" class="item_c col_red">￥{{(rate*month).toFixed(2)}}</view>
				<view v-if="value3==2" class="item_c col_red">￥{{(rate*week).toFixed(2)}}</view>
				<view v-if="value3==1" class="item_c col_red">￥{{(rate*day).toFixed(2)}}</view>
			</view>
			<view class="item" v-if="huanType==1||huanType==2">
				<view>到期需还本金</view>
				<view class="item_c col_red">￥{{money}}</view>
			</view>
			<!-- <view class="item" v-if="huanType==2">
				<view>到期需还本金{{month}}</view>
				<view class="item_c col_red">￥{{money}}</view>
			</view> -->
			<!-- <view class="item">
				<view>成交服务费</view>
				<view class="item_c">1980.00</view>
			</view> -->
		</view>

		<view class="info lend">
			<view class="jie">借款描述：</view>
			<textarea placeholder="请输入详细借款用途" v-model="description"></textarea>
		</view>
		<view v-if="disabled" class="btn" @click="edit()">修改申请</view>
		<view v-else class="btn" @click="passwordYn">提交审核</view>
		<view class="tongyi">
			<uni-data-checkbox multiple v-model="checkbox1" :localdata="hobby"></uni-data-checkbox>
			<view class="">同意<text @click="word(4)">《借款协议》</text></view>
		</view>

		<uni-popup ref="popup" background-color="#fff" @change="change3">
			<view class="box">
				<view class="text">请输入二级密码</view>
				<input type="safe-password" password="true" v-model="payPsd" />
				<view class="btns">
					<view class="" @click="cancel">取消</view>
					<view class="" @click="yes">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		selectInfo,
		applyLoan,
		payPassword,
		queryApply,
		editApply
	} from "@/common/js/index.js"
	export default {
		data() {
			return {
				title: 'Hello',
				topheight: uni.getStorageSync('topHeight'),
				totalheight: uni.getStorageSync('totalHeight'),
				navheight: uni.getStorageSync('navHeight'),
				imagelist: [
					require('@/static/img/home_bg3.png'),
					require('@/static/img/home_bg4.png')
				],
				barshow: false,
				speed: 50,
				text: '为了确保是您本人操作，保障您的资金安全，首先请进行实名认证',
				hobby: [{
					text: '',
					value: 0
				}],
				checkbox1: '',
				value: '',
				range: [],
				monthList: '',
				month: '',
				hint: '',
				value2: '',
				repayment: [],
				money_max: '',
				money_min: '',
				name: '',
				card: '',
				phone: '',
				money: '',
				rate: '',
				description: '',
				huanType: '',
				payPsd: '',
				range2: [{
					value: 1,
					text: '天'
				}, {
					value: 2,
					text: '周'
				}, {
					value: 3,
					text: '月'
				}],
				value3: 1,
				day: '',
				week: '',
				day_demo: "",
				d_text1: '按日还本息',
				d_text2: '每日还息,到期还本',
				w_text1: '按周还本息',
				w_text2: '每周还息,到期还本',
				m_text1: '按月还本息',
				m_text2: '每月还息,到期还本',
				deal_id: '',
				disabled: false
			}
		},
		onLoad(e) {
			console.log(e);
			if(e.type) {
				this.deal_id = e.id
				this.query()
			}
			this.select()
		},
		methods: {
			query() {
				queryApply({deal_id: this.deal_id}).then(res=>{
					console.log(res);
					var info = res.data.deal_info
					this.money = info.borrow_amount
					this.name = info.user.real_name
					this.card = info.user.idno
					this.phone = info.user.mobile
					this.value = info.type_id
					this.value3 = info.repay_time_type
					this.rate = info.interest
					this.value2 = info.loantype
					this.day = info.repay_time
					this.week = info.repay_time
					this.month = info.repay_time
					this.day_demo = info.week
					this.description = info.description
					this.disabled = true
				})
			},
			word(e) {
				uni.navigateTo({
					url:'/pages/index/word?type='+e
				})
			},
			checkInput() {
				let value = parseInt(this.money);
				if (value < this.money_min || value > this.money_max || value % 50 !== 0) {
					uni.showToast({
						title: '请输入正确的贷款金额',
						icon: 'none'
					});
					this.money = '';
				}
			},
			cancel() {
				this.$refs.popup.close()
			},
			passwordYn() {
				console.log(this.value2);
				var hint
				if (!this.name) {
					hint = '请输入借款人姓名'
				} else if (!hint && !this.card) {
					hint = '请输入身份证号'
				} else if (!hint && !this.phone) {
					hint = '请输入手机号'
				} else if (!hint && !this.value) {
					hint = '请选择借款用途'
				} else if (!hint && !this.value3) {
					hint = '请选择借款周期'
				} else if (!hint && !this.rate) {
					hint = '请输入利息'
				} else if (!hint && this.value2 ==='') {
					hint = '请选择还款方式'
				} else if (!hint && !this.description) {
					hint = '请输入借款描述'
				} else if (!hint && !this.checkbox1) {
					hint = '请先阅读并同意《借款协议》'
				}
				if (!hint && !this.checkbox1) {
					if(this.value3==1&&!this.day)hint = '请输入借款期限'
					else if(this.value3==2&&(!this.week||this.day_demo))hint = '请完善借款期限'
					else if(this.value3==3&&!this.month)hint = '请选择借款期限'
				}
				if (hint) {
					uni.showToast({
						title: hint,
						icon: 'none',
						duration: 3000
					})
					return
				}
				this.$refs.popup.open('center')
			},
			yes() {
				payPassword({
					paypassword: this.payPsd
				}).then(res => {
					console.log(res);
					this.cancel()
					if (res.code == 200) {
						this.apply()
					} else {
						this.payPsd = ''
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			changez(e) {
				console.log(e);
				if (e == 1) {
					this.repayment[0].text = this.d_text1
					this.repayment[1].text = this.d_text2
				} else if (e == 2) {
					this.repayment[0].text = this.w_text1
					this.repayment[1].text = this.w_text2
				} else if (e == 3) {
					this.repayment[0].text = this.m_text1
					this.repayment[1].text = this.m_text2
				}
			},
			change3() {},
			change(e) {
				console.log("e:", e);
			},
			change2(e) {
				console.log("e:", e);
				this.huanType = e
				this.value2 = e
			},
			edit() {
				var time = this.value3 == 1 ? this.day : this.value3 == 2 ? this.week : this.month
				var data = {
					deal_id: this.deal_id,
					type_id: this.value,
					repay_time: time,
					loantype: this.value2,
					description: this.description,
					repay_time_type: this.value3,
					week:this.day_demo,
					interest:this.rate
					
					// real_name: this.name,
					// idno: this.card,
					// mobile: this.phone,
					// borrow_amount: this.money,
					// rate: this.rate,
				}
				editApply(data).then(res => {
					console.log(res);
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
							// uni.redirectTo({
							// 	url: '/pages/index/audit?id=' + res.data.deal_id + '&userId=' + res
							// 		.data.user_id
							// })
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			apply() {
				var time = this.value3 == 1 ? this.day : this.value3 == 2 ? this.week : this.month
				var data = {
					real_name: this.name,
					idno: this.card,
					mobile: this.phone,
					borrow_amount: this.money,
					type_id: this.value,
					repay_time: time,
					// rate: this.rate,
					key: this.value2,
					description: this.description,
					repay_time_type: this.value3,
					week:this.day_demo,
					interest:this.rate
				}
				applyLoan(data).then(res => {
					console.log(res);
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(function() {
							uni.redirectTo({
								url: '/pages/index/audit?id=' + res.data.deal_id + '&userId=' + res
									.data.user_id
							})
						}, 2000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			select() {
				selectInfo().then(res => {
					console.log(res);
					this.monthList = res.data.month_type
					this.month = this.monthList[0]
					this.hint = res.data.rate_text
					this.money_max = res.data.max_money
					this.money_min = res.data.min_money
					// this.rate = res.data.min_rate
					var list1 = res.data.deal_load_type
					for (var i = 0; i < list1.length; i++) {
						list1[i]
						var info = {
							value: list1[i].id,
							text: list1[i].name
						}
						this.range.push(info)
					}
					var list2 = res.data.repayment_method
					for (var i = 0; i < list2.length; i++) {
						list2[i]
						var info = {
							value: list2[i].key,
							text: list2[i].name
						}
						this.repayment.push(info)
					}
					this.repayment[0].text = this.d_text1
					this.repayment[1].text = this.d_text2
					this.value2 = this.repayment[0].value
				})
			}
		},
		onPageScroll(e) {
			if (e.scrollTop > 48) {
				this.barshow = true
			} else {
				this.barshow = false
			}
		},
	}
</script>

<style scoped lang="scss">
	.content {
		position: relative;
		overflow: hidden;
	}

	.liu {
		width: 694rpx;
		margin: 80rpx 28rpx 60rpx;
	}

	/deep/ .uni-noticebar {
		width: 690rpx;
		background-color: #FFF3EE;
		border-radius: 59rpx;
		padding-left: 80rpx;
	}

	.notice {
		margin: 0 30rpx;
		position: relative;

		image {
			position: absolute;
			left: 20rpx;
			top: 12rpx;
			width: 48rpx;
		}
	}

	.money {
		line-height: 110rpx;
		margin: 40rpx 30rpx 0;
		border-bottom: 2rpx solid #333333;
		font-weight: 700;

		display: flex;
		align-items: center;

		text {
			font-weight: 700;
			font-size: 40rpx;
		}

		input {
			font-size: 80rpx;
		}

		.pla_money {
			font-size: 28rpx;
			font-weight: 400;
		}
	}

	.hint {
		font-size: 24rpx;
		color: #999999;
		margin: 20rpx 30rpx 0;
	}

	.lend {
		overflow: hidden;
	}

	.info {
		margin: 40rpx 30rpx;
		padding: 0 30rpx 40rpx;
		background-color: white;
		background: #FFFFFF;
		border-radius: 12rpx;
		// overflow: hidden;

		.item {
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			border-bottom: 2rpx solid #EEEEEE;

			.item_c {
				font-weight: 500;
				flex: 1;
				text-align: right;
			}

			.lv {
				display: flex;
				align-items: center;
				justify-content: flex-end;

				input {
					margin-right: 10rpx;
				}
			}

			image {
				width: 28rpx;
				margin-left: 12rpx;
			}

			input {
				text-align: right;
				font-size: 26rpx;
			}
		}

		.jie {
			margin: 40rpx 0 30rpx;
		}

		.yues {
			display: flex;
			justify-content: space-between;

			view {
				width: 120rpx;
				height: 64rpx;
				line-height: 64rpx;
				border-radius: 35rpx;
				background: #F7F7F7;
				color: #999999;
				font-size: 28rpx;
				text-align: center;
			}

			.sel {
				background: #FF534E;
				color: white;
			}
		}

		.hint {
			margin: 20rpx 0 0 0 !important;
			color: #999999;
		}
	}

	.col_red {
		color: #FF534E;
	}

	/deep/.uni-select {
		margin-left: auto;
		width: 300rpx;
		text-align: right;
		border: none;
	}

	/deep/ .uni-select__selector-item {
		display: block;
		text-align: right !important;
	}

	textarea {
		width: 620rpx;
		padding: 14rpx 0 14rpx 20rpx;
		background: #F8F8F8;
		border-radius: 12rpx;
		font-size: 26rpx;
	}

	.btn {
		width: 460rpx;
		height: 80rpx;
		line-height: 80rpx;
		margin: 80rpx auto 30rpx;
		background: linear-gradient(to right, #FF5F3B 0%, #FF524F 100%);
		border-radius: 40rpx;
		font-weight: 500;
		font-size: 32rpx;
		color: #FFFFFF;
		text-align: center;
	}

	.tongyi {
		padding-bottom: 100rpx;
		text-align: center;
		display: flex;
		align-items: center;
		justify-content: center;

		text {
			color: #FF534E;
		}
	}

	/deep/ .uni-data-checklist {
		flex: 0;
	}

	/deep/ .uni-data-checklist .checklist-group .checklist-box {
		margin-right: 0;
	}

	.box {
		width: 600rpx;
		height: 552rpx;
		background: url('@/static/img/diong.png') no-repeat;
		background-size: contain;
		overflow: hidden;

		input {
			height: 80rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			margin: 0 40rpx;
			padding: 0 16rpx;
			border: 2rpx solid #F69D5E;
		}

		.text {
			margin: 196rpx 40rpx 30rpx;
			font-size: 28rpx;
		}

		.btns {
			display: flex;
			justify-content: space-between;

			view {
				width: 220rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 32rpx;
				font-weight: 500;
				border-radius: 40rpx;
				margin: 52rpx 40rpx;

				&:nth-of-type(1) {
					border: 2rpx solid #FFA19E;
					color: #FF534E;
				}

				&:nth-of-type(2) {
					background: linear-gradient(to right, #F69F60 0%, #FF706D 100%);
					color: white;
				}
			}
		}
	}

	/deep/ .uni-popup__wrapper {
		background-color: rgba(0, 0, 0, 0) !important;
	}
	.col_9 {
		color: #999999 !important;
	}
	/deep/ .uni-select--disabled {
		background-color: white;
	}
</style>